import axios from 'axios'
import * as Constant from './constant'
import qs from 'qs'
import URL from '@/api/api'
let httpUtil = axios.create({
  baseURL: process.env.API_ROOT,
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
})

httpUtil.interceptors.request.use(function (config) {
  // if (config.url !== Constant.URL_AUTH) {
  //   config.headers.Authorization = 'Bearer ' + sessionStorage.getItem(Constant.AUTH_ACCESS_TOKEN)
  // }
  // if (config.url === URL.sheUploadFile) { // 会议上传设置headers，formData
  //   config.headers['enctype'] = 'multipart/form-data'
  //   config.headers['custom-printDebugInfo'] = 'Y'
  //   const formData = new FormData()
  //   formData.append(config.data.filename, config.data.file)
  //   for (let k in config.data.param) { // 设置参数
  //     formData.append(`${k}`, config.data.param[`${k}`])
  //   }
  //   config.data = formData
  // }
  config.headers['Content-Type'] = 'application/json;charset=UTF-8';
  // config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  config.headers['X-Requested-With'] = 'XMLHttpRequest';

  return config
}, function (error) {
  return Promise.reject(error)
})

httpUtil.interceptors.response.use(function (response) {
  return response
}, function (error) {
  if (error.response && error.response.status == 401) {
    window.location.href = process.env.API_ROOT + '/login/cas'
    // window.location.href = process.env.API_ROOT + '/oauth2login';
  }
  return Promise.reject(error)
})
httpUtil.all = axios.all
function initAxiosIns () {
  return httpUtil
}

export const HTTP = initAxiosIns()
